package com.lightsh.things;

import org.jbox2d.common.Vec2;

import android.util.Log;
import android.view.MotionEvent;

import com.lightsh.gui.BaseScreen;
import com.lightsh.object.CircleObject;
import com.lightsh.screens.PlaneScreen;
import com.lightsh.utility.Debugger;

public class SurfaceBtn extends CircleObject{
	private boolean mbLeft = false;
	private int mOffsetX = 0;
	
	public SurfaceBtn(BaseScreen screen, float x, float y, float radius) {
		super(screen, x, y, radius);
		setVisible(false);
	}
	
	@Override
	public void update() {
		super.update();
		if(mParentScreen.isEnd()){
			if(!isVisible()){
				setVisible(true);
			}
		}
		
		if(mbLeft){
			if(--mOffsetX < -mRadius){
				mbLeft = false;
			}
		}else{
			if(++mOffsetX > mRadius){
				mbLeft = true;
			}
		}
	}
	
	
	@Override
	public void create() {
		super.create();
		mParentScreen.addSurfaceObject(this);
	}

	@Override
	public Vec2 getPosition() {
		return new Vec2(mPosition.x + mOffsetX, mPosition.y);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev, float worldScale, float screenX, float screenY) {
		Vec2 vec2 = new Vec2(ev.getX(), ev.getY());
		if(isVisible() && isPointInside(vec2, 1, 0, 0) && (ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN){
//			Debugger.logT("offset :" + mOffsetX + "  " + getPosition() + " --- " + vec2 + " in side!");
			if(mParentScreen.isEnd() && mParentScreen instanceof PlaneScreen){
				PlaneScreen screen = (PlaneScreen)mParentScreen;
				screen.restartGame();
			}
			return true;
		}
		return false;
	}
}
